package com.app.service.impl;

import com.app.common.AreasTool;
import com.app.common.IdCardNumberMethod;
import com.app.mapper.JobIntentionMapper;
import com.app.mapper.PositionMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.service.CustomerService;
import com.app.service.JobIntentionService;

import com.app.service.PositionService;
import com.app.util.TokenUtils;
import com.app.vo.JobIntentionVo;
import com.app.vo.ValueAndNameVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.docx4j.wml.U;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:职位实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class JobIntentionServiceImpl implements JobIntentionService {


    @Autowired
    JobIntentionMapper jobIntentionMapper;
    @Autowired
    CustomerService customerService;
    @Autowired
    PositionService positionService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    JobIntentionMapper jobIntentionService;
    @Autowired
    PositionMapper positionMapper;

    /***
     * 新增
     * @param model
     */
    @Override
    public void insert(JobIntentionModel model) {
        jobIntentionMapper.insert(model);
    }

    @Override
    public void update(JobIntentionModel intention) {
        jobIntentionMapper.updateById(intention);
    }

    @Override
    public void delete(int id) {

        jobIntentionMapper.deleteById(id);
    }

    @Override
    public List<JobIntentionModel> getIntentionListByCustomerId(Integer customerId) {
        QueryWrapper<JobIntentionModel> queryWrapper = new QueryWrapper<>();
        if (customerId != null) {
            queryWrapper.select().eq("customer_id", customerId);
        }
        List<JobIntentionModel> userList = jobIntentionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public JobIntentionModel getJobIntentionById(int id) {
        return jobIntentionMapper.selectById(id);
    }

    @Override
    public List<JobIntentionModel> getJobIntentionByPost(int post) {
        QueryWrapper<JobIntentionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("post", post).orderByDesc("crt_time");
        List<JobIntentionModel> userList = jobIntentionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Object getIntentionList(String token, Integer id) {

        CustomerModel cus = null;
        if (id != null && id > 0) {
            cus = customerService.getCustomerById(Integer.valueOf(id));
        } else {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return 405;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("mobile", mobile);
            List<UserModel> userList = userMapper.selectByMap(map);
            UserModel user = null;
            if (userList != null) {
                user = userList.get(0);
            }
            if (user == null) {
                return 405;
            }
            cus = customerService.getCustomerById(user.getCId());
        }
        List<JobIntentionModel> intentionList = new ArrayList<JobIntentionModel>();
        intentionList = getIntentionListByCustomerId(cus.getId());
        if (intentionList != null && intentionList.size() > 0) {
            for (int i = 0; i < intentionList.size(); i++) {

                intentionList.get(i).setPostName(positionService.getModelById(intentionList.get(i).getPost()).getJobTitle());
                intentionList.get(i).setSalaryMin(Integer.valueOf(intentionList.get(i).getSalaryMin()));
                intentionList.get(i).setSalaryMax(Integer.valueOf(intentionList.get(i).getSalaryMax()));
                for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                    if (Integer.valueOf(intentionList.get(i).getRegProvince()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                        intentionList.get(i).setRegProvince(AreasTool.AREA_VO_LIST.get(j).getData());
                        break;
                    }
                }
                for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {

                    if (Integer.valueOf(intentionList.get(i).getRegCity()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                        intentionList.get(i).setRegCity(AreasTool.AREA_VO_LIST.get(j).getData());
                        break;
                    }

                }
                for (int j = 0; j < AreasTool.AREA_VO_LIST.size(); j++) {
                    if (Integer.valueOf(intentionList.get(i).getRegArea()).equals(AreasTool.AREA_VO_LIST.get(j).getId())) {
                        intentionList.get(i).setRegArea(AreasTool.AREA_VO_LIST.get(j).getData());
                        break;
                    }
                }
            }

        }
        return intentionList;
    }

    @Override
    public Object getIntention(Integer id) {
        JobIntentionModel model = getJobIntentionById(Integer.valueOf(id));
        JobIntentionVo jobIntentionVo = new JobIntentionVo();
        if (model != null) {
            jobIntentionVo.setId(model.getId());
            jobIntentionVo.setPostNames(positionService.getModelById(model.getPost()).getJobTitle());
            jobIntentionVo.setSalaryMin(model.getSalaryMin());
            jobIntentionVo.setSalaryMax(model.getSalaryMax());
            jobIntentionVo.setPost(model.getPost());
            jobIntentionVo.setRegArea(model.getRegProvince() + "," + model.getRegCity() + "," + model.getRegArea());
            jobIntentionVo.setRecruitType(model.getRecruitType());
        }
        return jobIntentionVo;
    }

    @Override
    public List<ValueAndNameVo> jobTypePie() {
        List<ValueAndNameVo> vo = new ArrayList<>();
        for (int i = 1; i < 3; i++) {
            QueryWrapper<JobIntentionModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("recruit_type", i);
            Integer count = jobIntentionMapper.selectCount(queryWrapper);
            ValueAndNameVo vo1 = new ValueAndNameVo();
            if (i == 1) {
                vo1.setName("全职");
            } else {
                vo1.setName("兼职");
            }
            vo1.setValue(count);
            vo.add(vo1);
        }
        return vo;
    }

    @Override
    public Object avgSalary() {

        return null;
    }

    @Override
    public List<JobIntentionModel> selectAll() {
        return jobIntentionMapper.selectList(null);
    }

    @Override
    public List<JobIntentionModel> selectPost() {
        return jobIntentionMapper.selectPost();
    }

    @Override
    public Page<JobIntentionModel> seekersList(Integer page1, Integer limit,Integer streetId) {
        Page<JobIntentionModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);

        QueryWrapper<JobIntentionModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.orderByDesc("id");
        Page<JobIntentionModel> EnterpriseModel = jobIntentionMapper.selectPage(page, queryWrapper1);
        List<PositionModel> positionModels = positionMapper.selectList(null);
        for (int i = 0; i < EnterpriseModel.getRecords().size(); i++) {
            for (int i1 = 0; i1 < positionModels.size(); i1++) {
                if (EnterpriseModel.getRecords().get(i).getPost() == positionModels.get(i1).getId()) {
                    EnterpriseModel.getRecords().get(i).setPostName(positionModels.get(i1).getJobTitle());
                }
            }
            CustomerModel customerById = customerService.getCustomerById(EnterpriseModel.getRecords().get(i).getCustomerId());
            QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("c_id", customerById.getId());
            UserModel model = userMapper.selectOne(queryWrapper);
            if (model != null && model.getMobile() != null) {
                customerById.setMobile(model.getMobile());
            }
            if (customerById.getSex() == 1) {
                customerById.setSexText("男");
            }
            if (customerById.getSex() == 2) {
                customerById.setSexText("女");
            }
            if (customerById.getSex() == 0) {
                customerById.setSexText("无");
            }
            EnterpriseModel.getRecords().get(i).setCustomer(customerById);
        }
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public List<JobIntentionModel> supplyAndDemandIndustries() {
        return jobIntentionMapper.supplyAndDemandIndustries();
    }

    @Override
    public int getCountByGroup() {
        QueryWrapper<JobIntentionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT customer_id");
        return jobIntentionMapper.selectCount(queryWrapper);
    }
}
